<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">


      <template #origin_price="{ record }">
        {{ tool.toFixed(record.origin_price, 2, 100) }}
      </template>
      <template #price="{ record }">
        {{ tool.toFixed(record.price, 2, 100) }}
      </template>
      <template #rmb_price="{ record }">
        {{ tool.toFixed(record.rmb_price, 2, 100) }}
      </template>

      <template #cover="{ record }">
        <a-avatar shape="square">
          <img :src="record.cover?record.cover:'/nopic.png'" style="object-fit: cover" />
        </a-avatar>
      </template>

      <template #create_time="{ record }">
        {{ tool.dateFormat(record.create_time, 'yyyy-MM-dd hh:mm:ss', '-') }}
      </template>

      <template #up_status="{ record }">
        <a-switch
            :checked-value="1"
            unchecked-value="2"
            @change="switchStatus($event, record.id,'up_status')"
            :default-checked="record.up_status == 1"
        />
      </template>


      <template #status="{ record }">
        <a-switch
            :checked-value="1"
            unchecked-value="2"
            @change="switchStatus($event, record.id,'status')"
            :default-checked="record.status == 1"
        />
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import configProduct from '@/api/config/configProduct'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()


const numberOperation = (newValue, id, numberName) => {
  configProduct.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  configProduct.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'us_config_product',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: false,
  operationColumnWidth: 160,
  bordered:{ wrapper: true, cell: true },
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: configProduct.getList,
  recycleApi: configProduct.getRecycleList,

})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    width:80,
    addDisplay: false,
    editDisplay: false,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },
  {
    title: "产品名称",
    dataIndex: "name",
    width:280,
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入产品名称"
    }
  },
  {
    title: "类型",
    dataIndex: "source_type",
    formType: "radio",
    search: true,
    commonRules: {
      required: true,
      message: "请输入类型"
    },
    dict: { data: [{ label: '商品', value: 2 }, { label: 'COGO', value: 1 }], translation: true }
  },
  {
    title: "原价",
    dataIndex: "origin_price",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入原价（从ZBT采集下来的price）"
    }
  },
  {
    title: "溢价",
    dataIndex: "price",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入溢价（ZBT平台的原价*溢价比例）平台自己用的"
    }
  },
  {
    title: "人民币价格",
    dataIndex: "rmb_price",
    width: 120,
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入汇率之后的人民币价格"
    }
  },

  {
    title: "原名称",
    dataIndex: "origin_name",
    formType: "input",
    width:220,
  },
  {
    title: "英文名称",
    dataIndex: "english_name",
    formType: "input",
    width:220,
  },
  {
    title: "商品图片",
    dataIndex: "cover",
    formType: "input"
  },
  {
    title: "库存",
    dataIndex: "stock",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入库存"
    }
  },
  {
    title: "饰品颜色",
    dataIndex: "tag_exterior",
    formType: "input"
  },
  {
    title: "饰品类型-英文",
    dataIndex: "eng_type",
    formType: "input",
    hide: true
  },

  {
    title: "分类名称",
    dataIndex: "category_name",
    formType: "input",
    search: false
  },
  {
    title: "唯一值（对应三方键值）",
    dataIndex: "item_id",
    formType: "input",
    width:220,
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "扩展字段",
    dataIndex: "extend_field",
    formType: "input",
    hide: true
  },
  {
    title: "物品等级",
    dataIndex: "grade",
    formType: "input"
  },

  {
    title: "上下架",
    dataIndex: "up_status",
    formType: "radio",
    search: false,
    commonRules: {
      required: true,
      message: "请输入上下架"
    },
    dict: {
      name: "data_up_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },

  {
    title: "状态",
    dataIndex: "status",
    formType: "radio",
    search: false,
    commonRules: {
      required: true,
      message: "请输入状态"
    },
    dict: {
      name: "data_show_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入创建者"
    }
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width:180,
    formType: "input",
    search: false,
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "更新时间",
    dataIndex: "update_time",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "",
    dataIndex: "delete_time",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default { name: 'config:product' } </script>